<%@taglib prefix="itrust" uri="/WEB-INF/tags.tld"%>
<%@page errorPage="/auth/exceptionHandler.jsp"%>

<%@page import="edu.ncsu.csc.itrust.action.EditOfficeVisitAction"%>
<%@page import="edu.ncsu.csc.itrust.beans.OfficeVisitBean"%>
<%@page import="edu.ncsu.csc.itrust.beans.PatientReferralBean"%>

<%@include file="/global.jsp"%>

<%
pageTitle = "iTrust - Edit Patient Referral Information";
%>

<%@include file="/header.jsp"%>

<%
boolean makeChange = false;
if (request.getParameter("change") != null) {
	if (request.getParameter("change").equalsIgnoreCase("True")) {
		makeChange = true;		
	}
}

String ovIDString = request.getParameter("ovID");
String pidString = (String)session.getAttribute("pid");
long refID = Long.parseLong(request.getParameter("refID"));

EditOfficeVisitAction officeAction = new EditOfficeVisitAction(prodDAO, loggedInMID, pidString, ovIDString);
OfficeVisitBean ov = officeAction.getOfficeVisit();

PatientReferralBean referral = null;
for (PatientReferralBean rbean : officeAction.getPatientReferralsForOfficeVisit() ) {
	if (rbean.getId() == refID) {
		referral = rbean;
		break;
	}
}
if (referral == null) {%>
<h1>ERROR: Referral not found.</h1>
<%}else{%>
<%if (!makeChange) { %>
<%// This portion prints a form to edit the prescription information. %>
<form
	action="editPatientReferral.jsp?refID=<%= StringEscapeUtils.escapeHtml("" + (refID)) %>&ovID=<%= StringEscapeUtils.escapeHtml("" + (ovIDString)) %>&change=True"
	method=post>
<table align=center border=1 class="fTable">
	<tr>
		<th colspan=5>Edit Patient Referral</th>
	</tr>
	<tr class="subHeader">
		<td align=center>Send To</td>
		<td align=center>Priority</td>
		<td align=center>Notes</td>
	</tr>
	<tr>
		<td align=center><input type="text" name="referTo" id="referTo"
			value=<%= StringEscapeUtils.escapeHtml("" + (referral.getReceiverID() )) %>
			maxlength=100></td>
		<td align=center><select name="priority" id="priority"
			style="font-size: 10px;">
			<option
				value="<%= StringEscapeUtils.escapeHtml("" + (referral.getPriority() )) %>">
			<%= StringEscapeUtils.escapeHtml("" + (referral.getPriority() )) %></option>
			<option value="">-- Please Select a Priority --</option>
			<option value="1">1 - most urgent</option>
			<option value="2">2 - moderately urgent</option>
			<option value="3">3 - least urgent</option>
		</select></td>
		<td align=center><textarea name="referNotes" rows="2" cols="20"><%= StringEscapeUtils.escapeHtml("" + (referral.getReferralDetails() )) %></textarea></td>
	</tr>
</table>
<br />
<center><input type="submit" value="Change"> <input
	type="reset" value="Reset"></center>
</form>

<%} else {%>
<%// This portion prints out the recently edited referral information and provides a link to the home page.
		try {
			referral.setId(Integer.parseInt(request.getParameter("refID")));
			referral.setReceiverID(Long.parseLong(request.getParameter("referTo")));
			referral.setPriority(Integer.parseInt(request.getParameter("priority")));
			referral.setReferralDetails(request.getParameter("referNotes"));
			officeAction.editPatientReferral(referral);
			loggingAction.logEvent(TransactionType.EDIT_PATIENT_REFERRAL, loggedInMID, ov.getPatientID(),
					"Office Visit ID: "+ ov.getID() + ", Referred To: " + request.getParameter("referTo"));			
			response.sendRedirect("http://localhost:8080/iTrust/auth/hcp-uap/editOfficeVisit.jsp?ovID=" + (ov.getID()));
			
		} catch(Exception e) {
			%><center><b>The Referral could not be edited due to an
internal error.<br />
Please Try again later.<br /><%= StringEscapeUtils.escapeHtml("" + (e.toString() )) %></b></center>
<%
		}
		
		%>
<%}%>
<%}%>
<%@include file="/footer.jsp"%>